home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / Development / PowerD / Dmod / dmod_MUI / examples / Class2.d < prev    next >
Encoding:
Text File  |  2002-10-28  |  9.2 KB  |  329 lines

  1. /*
  2. ** Demosource on how to use customclasses in D.
  3. ** Based on the C example 'Class2.c' by Stafan Stuntz.
  4. ** Translated TO E by Sven Steiniger
  5. ** Translated to D by Martin <MarK> Kuchinka
  6. */
  7.  
  8. OPT OPTIMIZE
  9.  
  10. MODULE 'muimaster','libraries/mui',
  11.        'intuition/classes','intuition/classusr','intuition/screens','intuition/intuition',
  12.        'utility','utility/tagitem',
  13.        'lib/amiga'
  14.  
  15. /***************************************************************************/
  16. /* Here is the beginning of our simple new class...                        */
  17. /***************************************************************************/
  18.  
  19. /*
  20. ** This class is the same as within Class1.c except that it features
  21. ** a pen attribute.
  22. */
  23.  
  24. OBJECT mydata
  25.     penspec:MUI_PenSpec,
  26.     pen:LONG,
  27.     penchange
  28.  
  29. CONST    MYATTR_PEN=$8022   /* tag value for the new attribute.            */
  30.  
  31.  
  32. PROC mNew(cl:PTR TO IClass,obj:PTR TO _Object,msg:PTR TO OpSet)(L)
  33.     DEF    data:PTR TO mydata,
  34.             tags:PTR TO TagItem,
  35.             tag:PTR TO TagItem
  36.  
  37.     IFN obj:=DoSuperMethodA(cl,obj,msg) THEN RETURN NIL
  38.  
  39.     data:=INST_DATA(cl,obj)
  40.  
  41.     /* parse initial taglist */
  42.     tags:=msg.AttrList
  43.     WHILE tag:=NextTagItem(&tags)
  44.         IF tag.Tag=MYATTR_PEN THEN
  45.             IF tag.Data THEN CopyMem(tag.Data,data.penspec,SIZEOF_MUI_PenSpec)
  46.     ENDWHILE
  47.  
  48. ENDPROC obj
  49.  
  50.  
  51. /* OM_NEW didnt allocates something, just DO nothing here... */
  52.  
  53. PROC mDispose(cl:PTR TO IClass,obj:PTR TO _Object,msg:PTR TO Msg)(L) IS
  54.     DoSuperMethodA(cl,obj,msg)
  55.  
  56.  
  57. PROC mSet(cl:PTR TO IClass,obj:PTR TO _Object,msg:PTR TO OpSet)(L)
  58.     DEF    data:PTR TO mydata,
  59.             tags:PTR TO TagItem,
  60.             tag:PTR TO TagItem
  61.  
  62.     data:=INST_DATA(cl,obj)
  63.     tags:=msg.AttrList
  64.     WHILE tag:=NextTagItem(&tags)
  65.         IF tag.Tag=MYATTR_PEN THEN
  66.             IF tag.Data
  67.                 CopyMem(tag.Data,data.penspec,SIZEOF_MUI_PenSpec)
  68.                 data.penchange:=TRUE
  69.                 MUI_Redraw(obj,MADF_DRAWOBJECT)  /* redraw ourselves completely */
  70.             ENDIF
  71.     ENDWHILE
  72.  
  73. ENDPROC DoSuperMethodA(cl,obj,msg)
  74.  
  75.  
  76. /*
  77. ** OM_GET method, see if someone wants to read the color.
  78. */
  79.  
  80. PROC mGet(cl:PTR TO IClass,obj:PTR TO _Object,msg:PTR TO OpGet)(L)
  81.     DEF    data:PTR TO mydata,storage:PTR TO LONG
  82.  
  83.     IF msg.AttrID=MYATTR_PEN
  84.         data:=INST_DATA(cl,obj)
  85.         storage:=msg.Storage
  86.         storage[]:=data.penspec
  87.         RETURN MUI_TRUE
  88.     ENDIF
  89.  
  90. ENDPROC DoSuperMethodA(cl,obj,msg)
  91.  
  92.  
  93. PROC mSetup(cl:PTR TO IClass,obj:PTR TO _Object,msg:PTR TO Msg)(L)
  94.     DEF    data:PTR TO mydata
  95.  
  96.     IFN DoSuperMethodA(cl,obj,msg) THEN RETURN FALSE
  97.     data:=INST_DATA(cl,obj)
  98.     data.pen:=MUI_ObtainPen(muiRenderInfo(obj),data.penspec,0)
  99.  
  100. ENDPROC MUI_TRUE
  101.  
  102.  
  103. PROC mCleanup(cl:PTR TO IClass,obj:PTR TO _Object,msg:PTR TO Msg)(L)
  104.     DEF    data:PTR TO mydata
  105.  
  106.     data:=INST_DATA(cl,obj)
  107.     MUI_ReleasePen(muiRenderInfo(obj),data.pen)
  108.  
  109. ENDPROC DoSuperMethodA(cl,obj,msg)
  110.  
  111.  
  112. /*
  113. ** AskMinMax method will be called before the window is opened
  114. ** and before layout takes place. We need to tell MUI the
  115. ** minimum, maximum and default size of our object.
  116. */
  117.  
  118. PROC mAskMinMax(cl:PTR TO IClass,obj:PTR TO _Object,msg:PTR TO MUIP_AskMinMax)(L)
  119.  
  120.     /*
  121.     ** let our superclass first fill in what it thinks about sizes.
  122.     ** this will e.g. add the size OF frame and inner spacing.
  123.     */
  124.  
  125.     DoSuperMethodA(cl,obj,msg)
  126.  
  127.     /*
  128.     ** now add the values specific TO our object. note that we
  129.     ** indeed need TO *add* these values, not just set them!
  130.     */
  131.  
  132.     msg.MinMaxInfo.MinWidth += 100
  133.     msg.MinMaxInfo.DefWidth += 120
  134.     msg.MinMaxInfo.MaxWidth += 500
  135.  
  136.     msg.MinMaxInfo.MinHeight += 40
  137.     msg.MinMaxInfo.DefHeight += 90
  138.     msg.MinMaxInfo.MaxHeight += 300
  139.  
  140. ENDPROC 0
  141.  
  142.  
  143. /*
  144. ** Draw method is called whenever MUI feels we should render
  145. ** our object. This usually happens after layout is finished
  146. ** or when we need to refresh in a simplerefresh window.
  147. ** Note: You may only render within the rectangle
  148. **       _mleft(obj), _mtop(obj), _mwidth(obj), _mheight(obj).
  149. */
  150.  
  151. PROC mDraw(cl:PTR TO IClass,obj:PTR TO _Object,msg:PTR TO MUIP_Draw)(L)
  152.     DEF    data:PTR TO mydata,
  153.             i
  154.  
  155.     data:=INST_DATA(cl,obj)
  156.  
  157.     /*
  158.     ** let our superclass draw itself first, area class would
  159.     ** e.g. draw the frame and clear the whole region. What
  160.     ** it does exactly depends on msg.flags.
  161.     */
  162.  
  163.     DoSuperMethodA(cl,obj,msg)
  164.  
  165.     /*
  166.     ** IF MADF_DRAWOBJECT isn't set, we shouldn't draw anything.
  167.     ** MUI just wanted TO update the frame OR something like that.
  168.     */
  169.  
  170.     IFN msg.flags & MADF_DRAWOBJECT THEN RETURN 0
  171.  
  172.     /*
  173.     ** test IF someone changed our pen
  174.     */
  175.  
  176.     IF data.penchange
  177.         data.penchange:=FALSE
  178.         MUI_ReleasePen(muiRenderInfo(obj),data.pen)
  179.         data.pen:=MUI_ObtainPen(muiRenderInfo(obj),data.penspec,0)
  180.     ENDIF
  181.  
  182.  
  183.     /*
  184.     ** ok, everything ready TO render...
  185.     ** Note that we *must* use the MUIPEN() macro before actually
  186.     ** using pens from MUI_ObtainPen() in rendering calls.
  187.     */
  188.  
  189.     SetAPen(_rp(obj),MUIPEN(data.pen))
  190.  
  191.     FOR i:=_mleft(obj) TO _mright(obj) STEP 5
  192.         Move(_rp(obj),_mleft(obj),_mbottom(obj))
  193.         Draw(_rp(obj),i,_mtop(obj))
  194.         Move(_rp(obj),_mright(obj),_mbottom(obj))
  195.         Draw(_rp(obj),i,_mtop(obj))
  196.     ENDFOR
  197.  
  198. ENDPROC 0
  199.  
  200.  
  201. /*
  202. ** Here comes the dispatcher FOR our custom class.
  203. ** Unknown/unused methods are passed to the superclass immediately.
  204. */
  205.  
  206. PROC MyDispatcher(cl:PTR TO IClass IN a0,obj IN a2,msg:PTR TO Msg IN a1)(LONG)
  207.  
  208.     SELECT msg.MethodID
  209.     CASE OM_NEW          ;  RETURN mNew      (cl,obj,msg)
  210.     CASE OM_DISPOSE      ;  RETURN mDispose  (cl,obj,msg)
  211.     CASE OM_SET          ;  RETURN mSet      (cl,obj,msg)
  212.     CASE OM_GET          ;  RETURN mGet      (cl,obj,msg)
  213.     CASE MUIM_AskMinMax  ;  RETURN mAskMinMax(cl,obj,msg)
  214.     CASE MUIM_Setup      ;  RETURN mSetup    (cl,obj,msg)
  215.     CASE MUIM_Cleanup    ;  RETURN mCleanup  (cl,obj,msg)
  216.     CASE MUIM_Draw       ;  RETURN mDraw     (cl,obj,msg)
  217.     ENDSELECT
  218.  
  219. ENDPROC DoSuperMethodA(cl,obj,msg)
  220.  
  221.  
  222.  
  223. /***************************************************************************/
  224. /* Thats all there is about it. Now lets see how things are used...        */
  225. /***************************************************************************/
  226.  
  227. DEF    MUIMasterBase,UtilityBase
  228.  
  229. PROC main()
  230.     DEF    app=NIL,window,myobj,pen,
  231.             mcc=NIL:PTR TO MUI_CustomClass,
  232.             startpen:PTR TO MUI_PenSpec,
  233.             sigs=0
  234.  
  235.     IFN MUIMasterBase:=OpenLibrary(MUIMASTER_NAME, MUIMASTER_VMIN) THEN
  236.         Raise('Failed to open muimaster.library')
  237.  
  238.     /*
  239.     ** open utility.library, because we need function NextTagItem()
  240.     */
  241.     IFN UtilityBase:=OpenLibrary('utility.library',36) THEN
  242.         Raise('Failed to open utility.library')
  243.  
  244.  
  245.     /* Create the NEW custom class with a call TO MUI_CreateCustomClass(). */
  246.     /* Caution: This function returns not a struct IClass, BUT a           */
  247.     /* struct MUI_CustomClass which contains a struct IClass TO be         */
  248.     /* used with NewObject() calls.                                        */
  249.     /* Note well: MUI creates the dispatcher hook FOR you, you may         */
  250.     /* *not* use its h_Data field! IF you need custom data, use the        */
  251.     /* cl_UserData OF the IClass structure!                                */
  252.  
  253.     IFN mcc:=MUI_CreateCustomClass(NIL,MUIC_Area,NIL,SIZEOF_mydata,&MyDispatcher) THEN
  254.         Raise('Could not create custom class.')
  255.  
  256.     app:=ApplicationObject,
  257.         MUIA_Application_Title,      'Class2',
  258.         MUIA_Application_Version,    '$VER: Class2 12.9 (21.11.95)',
  259.         MUIA_Application_Copyright,  '©1995, Stefan Stuntz',
  260.         MUIA_Application_Author,     'Stefan Stuntz',
  261.         MUIA_Application_Description,'Demonstrate the use of custom classes.',
  262.         MUIA_Application_Base,       'CLASS2',
  263.  
  264.         SubWindow, window:=WindowObject,
  265.             MUIA_Window_Title,'Another Custom Class',
  266.             MUIA_Window_ID,"CLS2",
  267.             WindowContents,VGroup,
  268.                 Child, TextObject,
  269.                     TextFrame,
  270.                         MUIA_Background,MUII_TextBack,
  271.                         -> E-note : center this text means inserting a <ESC c> which is usually \ec
  272.                         MUIA_Text_Contents,
  273.                             '\ecThis is a custom class with attributes.\n'+
  274.                             'Click on the button at the bottom of\n'+
  275.                             'the window to adjust the color.',
  276.                     End,
  277.                     Child,myobj:=NewObjectA(mcc.Class,NIL,[TextFrame,
  278.                         MUIA_Background, MUII_BACKGROUND,
  279.                         TAG_DONE]),
  280.                     Child,HGroup,MUIA_Weight,10,
  281.                     Child,FreeLabel('Custom Class Color:'),
  282.                     Child,pen:=PoppenObject,
  283.                         MUIA_CycleChain,1,
  284.                         MUIA_Window_Title,'Custom Class Color',
  285.                     End,
  286.                 End,
  287.             End,
  288.         End,
  289.     End
  290.  
  291.     IFN app THEN Raise('Failed to create Application.')
  292.  
  293.     DoMethodA(window,[MUIM_Notify,MUIA_Window_CloseRequest,MUI_TRUE,
  294.         app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit])
  295.  
  296.     DoMethodA(pen,[MUIM_Notify,MUIA_Pendisplay_Spec,MUIV_EveryTime,
  297.         myobj,3,MUIM_Set,MYATTR_PEN,MUIV_TriggerValue])
  298.  
  299.     get(pen,MUIA_Pendisplay_Spec,&startpen)
  300.     set(myobj,MYATTR_PEN,startpen)
  301.  
  302. /*
  303. ** This is the ideal input loop for an object oriented MUI application.
  304. ** Everything is encapsulated in classes, no return ids need to be used,
  305. ** we just check if the program shall terminate.
  306. ** Note that MUIM_Application_NewInput expects sigs to contain the result
  307. ** from Wait() (or 0). This makes the input loop significantly faster.
  308. */
  309.  
  310.     set(window,MUIA_Window_Open,MUI_TRUE)
  311.  
  312.     WHILE Not(DoMethodA(app,[MUIM_Application_NewInput,&sigs]) = MUIV_Application_ReturnID_Quit)
  313.         IF sigs THEN sigs := Wait(sigs)
  314.     ENDWHILE
  315.  
  316.     set(window,MUIA_Window_Open,FALSE)
  317.  
  318. /*
  319. ** Shut down...
  320. */
  321.  
  322. EXCEPTDO
  323.     IF app THEN MUI_DisposeObject(app)                /* dispose all objects. */
  324.     IF mcc THEN MUI_DeleteCustomClass(mcc)            /* delete the custom class. */
  325.     IF UtilityBase THEN CloseLibrary(UtilityBase)
  326.     IF MUIMasterBase THEN CloseLibrary(MUIMasterBase) /* close library */
  327.     IF exception THEN PrintF('\s\n',exception)
  328. ENDPROC
  329.